//
//  BallView.m
//  CiscoProject
//
//  Created by Stefano Pilla on 17/10/10.
//  Copyright 2010 Will Servizi Informatici. All rights reserved.
//

#import "BallView.h"


@implementation BallView

@synthesize BallViewImage;
@synthesize currentDisplayString;
@synthesize displayStrings;

- (id)init {
	// Retrieve the image for the view and determine its size
	UIImage *image = [UIImage imageNamed:@"ball45x45.png"];
	CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
	
	// Set self's frame to encompass the image
	if (self = [self initWithFrame:frame]) {
		
		self.opaque = NO;
		BallViewImage = image;
		
		// Load the display strings
		NSString *path = [[NSBundle mainBundle] pathForResource:@"Numbers" ofType:@"strings"];
		NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:NULL];
		self.displayStrings = [string componentsSeparatedByString:@"\n"];
		displayStringsIndex = 0;
		[self setupNextDisplayString];
	}
	return self;
}


- (void)dealloc {
	[BallViewImage release];
	[currentDisplayString release];
	[displayStrings release];
	[super dealloc];
}


#define STRING_INDENT 20

- (void)drawRect:(CGRect)rect {
	
	// Draw the placard at 0, 0
	[BallViewImage drawAtPoint:(CGPointMake(0.0, 0.0))];
	
	/*
	 Draw the current display string.
	 Typically you would use a UILabel, but this example serves to illustrate the UIKit extensions to NSString.
	 The text is drawn center of the view twice - first slightly offset in black, then in white -- to give an embossed appearance.
	 The size of the font and text are calculated in setupNextDisplayString.
	 */
	
	// Find point at which to draw the string so it will be in the center of the view
	CGFloat x = self.bounds.size.width/2 - textSize.width/2;
	CGFloat y = self.bounds.size.height/2 - textSize.height/2;
	CGPoint point;
	
	// Get the font of the appropriate size
	UIFont *font = [UIFont systemFontOfSize:fontSize];
	
	[[UIColor blackColor] set];
	point = CGPointMake(x, y + 0.5);
	[currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
	
	[[UIColor whiteColor] set];
	point = CGPointMake(x, y);
	[currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; 
}


- (void)setupNextDisplayString {
	
	// Get the string at the current index, then increment the index
	self.currentDisplayString = [displayStrings objectAtIndex:displayStringsIndex];
	displayStringsIndex++;
	if (displayStringsIndex >= [displayStrings count]) {
		displayStringsIndex = 0;
	}
	
	UIFont *font = [UIFont systemFontOfSize:24];
	// Precalculate size of text and size of font so that text fits inside placard
	textSize = [currentDisplayString sizeWithFont:font minFontSize:9.0 actualFontSize:&fontSize forWidth:(self.bounds.size.width-STRING_INDENT) lineBreakMode:UILineBreakModeMiddleTruncation];
	
	[self setNeedsDisplay];
}


@end

